<?php


namespace app\common\model\divide;


use app\common\model\Setting as SettingModel;
use think\Model;

class Store extends Model
{

    protected $name='divide_store';




    public static function getStatuslist(){
        return [0 =>'待分账', 1=> '已分账', '2' => '分账失败'];
    }
    public function wxapp(){
        return $this->belongsTo("app\common\model\Wxapp",'wxapp_id','wxapp_id');
    }

    /**
     * 从缓存中获取商城名称
     * @param $data
     * @return array
     */
    public function getStoreName($data)
    {
        $names = [];
        foreach ($data as $wxapp) {
            $names[$wxapp['wxapp_id']] = SettingModel::getItem('store', $wxapp['wxapp_id'])['name'];
        }
        return $names;
    }

    /**
     * 获取订单列表
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($query = [])
    {
        // 设置查询条件
        !empty($query) && $this->setQueryWhere($query);

        // 获取列表数据
        return $this->with(['wxapp'])
            ->alias('store')
            ->field('store.*')
            ->join('wxapp', 'wxapp.wxapp_id =store.wxapp_id')
            ->order(['store.create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

    /**
     * 设置查询条件
     * @param $query
     */
    private function setQueryWhere($query)
    {
        // 设置默认的检索数据
        $params = $this->setQueryDefaultValue($query, [
            'status' => '-1',
        ]);

        // 用户昵称/订单号
        if (isset($query['search']) && !empty($query['search'])) {
            $this->where('wxapp.wxapp_name', 'like', '%' . trim($query['search']) . '%');
        }
        // 支付状态
        $params['status'] > -1 && $this->where('store.status', '=', (int)$params['status']);
        // 起始时间
        !empty($params['start_time']) && $this->where('store.create_time', '>=', strtotime($params['start_time']));
        // 截止时间
        !empty($params['end_time']) && $this->where('store.create_time', '<', strtotime($params['end_time']) + 86400);

        if (isset($query['search_time']) && !empty($query['search_time'])) {
            $times=explode('至',$query['search_time']);
            $this->where('store.create_time','between time',$times);
        }
    }

    /**
     * 设置默认的检索数据
     * @param $query
     * @param array $default
     * @return array
     */
    protected function setQueryDefaultValue(&$query, $default = [])
    {
        $data = array_merge($default, $query);
        foreach ($query as $key => $val) {
            if (empty($val) && isset($default[$key])) {
                $data[$key] = $default[$key];
            }
        }
        return $data;
    }


    public function getIncomeList($query = []){
        // 设置查询条件
        !empty($query) && $this->setQueryIncomeWhere($query);

        // 获取列表数据
        return $this->with(['wxapp'])
            ->alias('store')
            ->field("store.wxapp_id,FROM_UNIXTIME(store.create_time,'%Y-%m-%d') as dates,sum(amount) as amounts ,sum(payment) as payments")
            ->join('wxapp', 'wxapp.wxapp_id =store.wxapp_id')
            ->order(['dates' => 'desc','store.wxapp_id'=>'asc'])
            ->group("FROM_UNIXTIME(store.create_time,'%Y-%m-%d'),store.wxapp_id")
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

    private function setQueryIncomeWhere($query)
    {
        // 设置默认的检索数据
        $params = $this->setQueryDefaultValue($query, [
            'status' => '-1',
        ]);

        // 用户昵称/订单号
        if (isset($query['search']) && !empty($query['search'])) {
            $this->where('wxapp.wxapp_name', 'like', '%' . trim($query['search']) . '%');
        }
        // 起始时间
        !empty($params['start_time']) && $this->where('store.create_time', '>=', strtotime($params['start_time']));
        // 截止时间
        !empty($params['end_time']) && $this->where('store.create_time', '<', strtotime($params['end_time']) + 86400);

        if (isset($query['search_time']) && !empty($query['search_time'])) {
            $times=explode('至',$query['search_time']);
            $this->where('store.create_time','between time',$times);
        }
    }

    public function getGather($query = []){
        // 设置查询条件
        !empty($query) && $this->setQueryIncomeWhere($query);

        // 获取列表数据
        return $this->with(['wxapp'])
            ->alias('store')
            ->field("store.wxapp_id,FROM_UNIXTIME(store.create_time,'%Y-%m') as dates,sum(amount) as amounts ,sum(payment) as payments")
            ->join('wxapp', 'wxapp.wxapp_id =store.wxapp_id')
            ->order(['dates' => 'desc','store.wxapp_id'=>'asc'])
            ->group("FROM_UNIXTIME(store.create_time,'%Y-%m'),store.wxapp_id")
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

}